package dialogs.game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import persistence.Player;

@SuppressWarnings("serial")
public class GameView extends javax.swing.JPanel implements Observer {
	
	private GameInputModel inputModel;
	private TableModel tableModel;
	private GameComboBoxModel comboModel;
	
	private JTextField goalsT1TextField;
	private JTextField goalsT2TextField;
	private JTextField dateTextField;
	private JComboBox<Player> team1Player1ComboBox;
	private JComboBox<Player> team1Player2ComboBox;
	private JComboBox<Player> team2Player1ComboBox;
	private JComboBox<Player> team2Player2ComboBox;
	private javax.swing.JButton addGameButton;
	private javax.swing.JButton clearFormButton;
	private javax.swing.JButton deleteButton;
	private javax.swing.JButton saveAsXMLButton;
	private javax.swing.JButton loadFromXMLButton;
	
	
	/**
	 * Farbe fuer gueltige Felder
	 */
	private Color validColor = new Color(255, 255, 255);

	/**
	 * Farbe fuer ungueltige Felder
	 */
	private Color invalidColor = new Color(255, 0, 0);


	private JTable GameTable;
	public GameView(GameInputModel inputModel,TableModel tableModel, GameComboBoxModel comboModel){
		this.inputModel = inputModel;
        this.tableModel = tableModel;
        this.comboModel = comboModel;
        // Anmelden der View (also diesem Objekt selbst) bei dem Modell
        // die PlayerView implementiert das Observer-Interface und
        // wird von der Observablen (also dem Modell), bei Veraenderungen 
        // des Modells benachrichtigt, indem in der PlayerView die Methode
        // update() aufgerufen wird (siehe unten)
        this.inputModel.addObserver(this);
    	initComponents();
	}
	private void initComponents() {
		goalsT1TextField =new JTextField();
		goalsT2TextField =new JTextField();
		dateTextField = new JTextField();
		
		
		//ComboBox-Objekten werden mit Hilfe des GameComboBoxModels Modelle zugewiesen
		team1Player1ComboBox = new JComboBox<Player>(comboModel.getPlayer1Team1ComboBoxModel());
		team1Player1ComboBox.setSelectedIndex(-1);	
		team1Player2ComboBox = new JComboBox<Player>(comboModel.getPlayer2Team1ComboBoxModel());
		team1Player2ComboBox.setSelectedIndex(-1);
		team2Player1ComboBox = new JComboBox<Player>(comboModel.getPlayer1Team2ComboBoxModel());
		team2Player1ComboBox.setSelectedIndex(-1);
		team2Player2ComboBox = new JComboBox<Player>(comboModel.getPlayer2Team2ComboBoxModel());
		team2Player2ComboBox.setSelectedIndex(-1);
		
		addGameButton = new JButton("Spiel hinzufuegen");
		addGameButton.setActionCommand("add");

		clearFormButton = new JButton("Formular leeren");
		clearFormButton.setActionCommand("clear");
		
		deleteButton = new JButton("Spiel loeschen(zu Testzwecken)");
		deleteButton.setActionCommand("delete");
		
		saveAsXMLButton = new JButton("Speichern als XML");
		saveAsXMLButton.setActionCommand("saveAsXML");
		
		loadFromXMLButton = new JButton("laden aus XML");
		loadFromXMLButton.setActionCommand("loadFromXML");
		
		GameTable = new JTable(tableModel);
        // Tabellen-Scrollbereich definieren
        GameTable.setPreferredScrollableViewportSize(new Dimension(500, 80));
        GameTable.setFillsViewportHeight(true);
        GameTable.setRowHeight(18);
        
		//EingabePanel mit GridBagLayout
        GridBagLayout dataLayout = new GridBagLayout();
        JPanel     dataPanel  = new JPanel();
        dataPanel.setLayout(dataLayout);
        //Datensubpanels fuer die Teams
        GridLayout teamLayout = new GridLayout(0,2);
        JPanel team1Panel= new JPanel(teamLayout);
        JPanel team2Panel= new JPanel(teamLayout);
        
        
        team1Panel.add(new JLabel("Spieler1:"));
        team1Panel.add(team1Player1ComboBox);
        team1Panel.add(new JLabel("Spieler2:"));
        team1Panel.add(team1Player2ComboBox);
        team1Panel.add(new JLabel("Tore:"));
        team1Panel.add(goalsT1TextField);
        
        team2Panel.add(new JLabel("Spieler1:"));
        team2Panel.add(team2Player1ComboBox);
        team2Panel.add(new JLabel("Spieler2:"));
        team2Panel.add(team2Player2ComboBox);
        team2Panel.add(new JLabel("Tore:"));
        team2Panel.add(goalsT2TextField);
        
        addComponent(dataPanel, dataLayout, new JLabel("Datum:"), 0, 0, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, dateTextField, 2, 0, 2, 1, 1, 1);
        addComponent(dataPanel, dataLayout, new JLabel("Team1",JLabel.CENTER), 0, 1, 2, 1, 1, 1);
        addComponent(dataPanel, dataLayout, new JLabel("Team2",JLabel.CENTER), 2, 1, 2, 1, 1, 1);
        addComponent(dataPanel, dataLayout, team1Panel, 0, 2, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, team2Panel, 3, 2, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, new JSeparator(), 0, 3, 4, 1, 1, 1);
        addComponent(dataPanel, dataLayout, addGameButton, 0, 4, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, clearFormButton, 3, 4, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, deleteButton, 0, 6, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, saveAsXMLButton, 0, 5, 1, 1, 1, 1);
        addComponent(dataPanel, dataLayout, loadFromXMLButton, 3, 5, 1, 1, 1, 1);
        // in der PlayerView ein BorderLayout als Layoutmanager setzen
        this.setLayout(new BorderLayout() );
        // Hinzufuegen des DatenPanels (mit den Eigabefeldern, und dem Button)
        this.add(dataPanel, BorderLayout.NORTH);
        // Hinzufuegen des scrollPane mit der Spieler-Tabelle
        JScrollPane scrollPane = new JScrollPane(GameTable);
        this.add(scrollPane, BorderLayout.CENTER);
        
        
		
	}
	
	/**
	 * Hilfmethode, um den Umgang mit GridBagLayout zu vereinfachen
	 * 
	 */
	private void addComponent(Container cont, GridBagLayout gbl, Component c,
			int x, int y, int width, int height, double weightx, double weighty) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbl.setConstraints(c, gbc);
		cont.add(c);
	}
	public void addActionListener(ActionListener al){
		this.addGameButton.addActionListener(al);
		this.clearFormButton.addActionListener(al);
		this.deleteButton.addActionListener(al);
		this.saveAsXMLButton.addActionListener(al);
		this.loadFromXMLButton.addActionListener(al);
	}
	@Override
	public void update(Observable o, Object arg){
		
		if(inputModel.isDateValid()) {
			dateTextField.setBackground(validColor);
		}
		else {
			dateTextField.setBackground(invalidColor);
		}
		if(inputModel.isGoalsT1Valid()) {
			goalsT1TextField.setBackground(validColor);
		}
		else {
			goalsT1TextField.setBackground(invalidColor);
		}
		if(inputModel.isGoalsT2Valid()) {
			goalsT2TextField.setBackground(validColor);
		}
		else {
			goalsT2TextField.setBackground(invalidColor);
		}
		if(inputModel.isPlayersValid()) {
			team1Player1ComboBox.setBackground(validColor);
			team1Player2ComboBox.setBackground(validColor);
			team2Player1ComboBox.setBackground(validColor);
			team2Player2ComboBox.setBackground(validColor);
		}
		else {
			team1Player1ComboBox.setBackground(invalidColor);
			team1Player2ComboBox.setBackground(invalidColor);
			team2Player1ComboBox.setBackground(invalidColor);
			team2Player2ComboBox.setBackground(invalidColor);
		}
	}
	
	public String getToreT1Text() {
		return goalsT1TextField.getText();
	}
	public void setToreT1Text(String text) {
		goalsT1TextField.setText(text);
	}
	public String getToreT2Text() {
		return goalsT2TextField.getText();
	}
	public void setToreT2Text(String text) {
		goalsT2TextField.setText(text);
	}
	public String getDateText() {
		return dateTextField.getText();
	}
	public void setDateText(String text) {
		dateTextField.setText(text);
	}
	public Player getTeam1Player1FromComboBox() {
		return (Player)team1Player1ComboBox.getSelectedItem();
	}
	public Player getTeam1Player2FromComboBox() {
		return (Player)team1Player2ComboBox.getSelectedItem();
	}
	public Player getTeam2Player1FromComboBox() {
		return (Player)team2Player1ComboBox.getSelectedItem();
	}
	public Player getTeam2Player2FromComboBox() {
		return (Player)team2Player2ComboBox.getSelectedItem();
	}
	
	public int getSelectedGameIndex() {
		if (GameTable.getSelectedRow() != -1)
			return GameTable.convertRowIndexToModel(GameTable
					.getSelectedRow());
		else
			return -1;
	}
	/**
	 * Methode loescht alle Eintraege in den Eingabefeldern
	 */
	public void refresh() {
		team1Player1ComboBox.setSelectedIndex(-1);
		team1Player2ComboBox.setSelectedIndex(-1);
		team2Player1ComboBox.setSelectedIndex(-1);
		team2Player2ComboBox.setSelectedIndex(-1);
		setToreT1Text("");
		setToreT2Text("");
		setDateText("");
	}
}